<template>
    <div class="w-4/5 mx-auto">
        <br />
        <el-form
            label-width="80px"
            :model="formData"
            :rules="formRules"
            ref="formComp"
            status-icon
        >
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="formData.pass"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input
                    type="password"
                    v-model="formData.checkPass"
                ></el-input>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
                <el-input v-model="formData.age"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm()">提交</el-button>
                <el-button @click="resetForm()">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, unref } from "vue";

export default defineComponent({
    name: "CompName",
    setup() {
        const formComp: any = ref(null);
        const formData = reactive({
            pass: "",
            checkPass: "",
            age: "",
        });
        const validatorPass = (_rule: any, _value: any, _callback: any) => {
            console.log("validatorPass",_value);
            if (_value === "") {
                _callback(new Error("请输入密码"));
            } else {
                if (formData.checkPass !== "") {
                    formComp.value.validateField("checkPass");
                }
                _callback();
            }
        };
        const validatorPass2 = (_rule: any, _value: any, _callback: any) => {
            console.log("validatorPass2",_value);
            if (_value === "") {
                _callback(new Error("请再次输入密码"));
            } else if (_value !== formData.pass) {
                _callback(new Error("两次输入密码不一致!"));
            } else {
                _callback();
            }
        };
        const validatorAge = (_rule: any, _value: any, _callback: any) => {
            console.log(_value);
            if (!_value) {
                return _callback(new Error("年龄不能为空"));
            }
            setTimeout(() => {
                if (!Number.isInteger(_value)) {
                    _callback(new Error("请输入数字值"));
                } else {
                    if (_value < 18) {
                        _callback(new Error("必须年满18岁"));
                    } else {
                        _callback();
                    }
                }
            }, 1000);
        };

        const formRules = {
            pass: [{ validator: validatorPass, trigger: "blur" }],
            checkPass: [{ validator: validatorPass2, trigger: "blur" }],
            age: [{ validator: validatorAge, trigger: "blur" }],
        };

        const submitForm = () => {
            const form: any = unref(formComp);
            form.validate()
                .then((res: any) => {
                    console.log("成功：", res);
                })
                .catch((err: any) => {
                    console.log("失败：", err);
                });
        };
        const resetForm = () => {
            const form: any = unref(formComp);
            form.resetFields();
        };
        return {
            formComp,
            formData,
            formRules,
            submitForm,
            resetForm,
        };
    },
});
</script>

<style scoped></style>
